package com.example.demo.repository;

import com.example.demo.entity.Order;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * Copyright (C), 2019-2019, XXX有限公司
 * FileName: OrderRepository
 * Author:   longzhonghua
 * Date:     2019/4/15 10:50
 *
 * @Description: $description$
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
public interface OrderRepository extends JpaRepository<Order, Long> {
    Order findByid(long id);
    Order findByOrderNo(String order_no);
    /**
     * 只能更新未支付的订单status=0
     * @param orderNo
     * @return
     */
    @Modifying
    @Query(value = "update Order o set o.status=2 where o.orderNo=:orderNo and o.status=0")
    int paySuccessOrder(@Param("orderNo") String orderNo);
    /**
     * 只能取消未支付的订单status=0
     * @param orderNo
     * @return
     */
    @Modifying
    @Query(value = "update Order o set o.status=1 where o.orderNo=:orderNo and o.status=0")
    int expireOrder(@Param("orderNo") String orderNo);
    List<Order> findByUserId(long userId);
}